package com.example.demo.LeetCode.list;

import javax.swing.*;

public class RemoveNthFromEnd {
    public static ListNode  removeNthFromEnd(ListNode head, int k){
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode p = dummy;
       for(int i = 0 ;i < k + 1 ; i++){
           p = p.next;
       }
       ListNode p2 = dummy;
       while (p != null){
           p = p.next;
           p2 = p2.next;
       }
       ListNode next = p2.next;
       p2.next = next.next;
       next.next = null;
       return dummy.next;

    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode p1 = new ListNode(2);
        ListNode p2 = new ListNode(3);
        ListNode p3 = new ListNode(4);
        ListNode p4 = new ListNode(5);
        ListNode p5 = new ListNode(6);
      //  head.next = p1;
      //  p1.next = p2;
      /*  p2.next = p3;
        p3.next = p4;
        p4.next = p5;*/
        ListNode p = removeNthFromEnd(head,1);
        while(p != null){
            System.out.print(p.val+", ");
            p = p.next;
        }
    }
}
